﻿using Microsoft.AspNetCore.Mvc;
using SiteApp.BLL.Interface;
using SiteApp.Web.Model;
using System.Linq;
using System.Threading.Tasks;

namespace SiteApp.Web.Controllers
{
    [ApiController]
    [Route("[controller]")]
    /// <summary>
    /// 与用户相关接口
    /// </summary>
    /// <returns></returns>
    public class LoginController : ControllerBase
    {

        private readonly IUserService userService = null;

        public LoginController(
             IUserService user)
        {
            this.userService = user;
        }



        private bool IsValid()
        {
            if (HttpContext.User.Claims.ToList() != null && HttpContext.User.Claims.ToList()[0].Type == "IsAuthenticated" && HttpContext.User.Claims.ToList()[0].Value == "true")
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("Submit")]
        public async Task<JsonResult> Submit([FromForm]string name, [FromForm] string pwd)
        {

          
            var result = new WebResult();

            if (IsValid())
            {
                result.Data = userService.SelectUser(name, pwd);
            }
 
           

            return new JsonResult(result);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdatePassword")]
        public async Task<JsonResult> UpdatePassword([FromForm] string userId, [FromForm] string oldPwd, [FromForm] string newPwd)
        {

            var result = new WebResult();
            if (IsValid())
            {

            result.Data = userService.UpdatePassword(userId, oldPwd, newPwd);

            }

            return new JsonResult(result);
        }



        /// <summary>
        /// 启用禁用
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateIsenable")]
        public async Task<JsonResult> UpdateIsenable([FromForm]  string userId, [FromForm] int isEnable)
        {

            var result = new WebResult();
            if (IsValid())
            {
                result.Data = userService.UpdateIsenable(userId, isEnable);
            }



            return new JsonResult(result);
        }


      







    }
}
